package com.sgb.kjwl.viewmodel.material;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.STFilterEntity;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.SearchSTEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MaterialRepertory;
import com.sgb.kjwl.view.ui.fragment.material.STListFragment3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 商砼料列表（1 筛选条件请求 2 列表数据请求）
 */
public class STFragListModel3 extends BaseViewModel {
    public ObservableField<MaterialFilterJson> materialFilterJson = new ObservableField<>(new MaterialFilterJson());
    private STListFragment3 fragment3;
    private MaterialRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public STFragListModel3(STListFragment3 fragment3, MaterialRepertory repertory) {
        this.fragment3 = fragment3;
        this.repertory = repertory;
    }

    public void getSTModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        materialFilterJson.get().setPageNum(page);
        materialFilterJson.get().setPageSize(size);
        materialFilterJson.get().setAreaCode(repertory.getAreaCode());
        DialogHelper.showProgressDialog2(fragment3.getContext(), null, "获取信息中...", 0, false, null, false);
//        repertory.getSTList(materialFilterJson.get())
//                .subscribe(new Consumer<BaseEntity<ListResponse<List<SearchSTEntity>>>>() {
//                    @Override
//                    public void accept(BaseEntity<ListResponse<List<SearchSTEntity>>> listResponseBaseEntity) throws Exception {
//                        DialogHelper.dismissProgressDialog();
//                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            fragment3.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        DialogHelper.dismissProgressDialog();
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });

        NewAPIManager.getInstance().getGoodsAPI().getSTList(materialFilterJson.get()).enqueue(new ICallBack<BaseEntity<ListResponse<List<SearchSTEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<SearchSTEntity>>> data) {
                fragment3.showList(data.getData().getList(), isLoadMore);
            }
        });
    }

    /*public void getSTFilterTypeModel() {
        repertory.getSTFilter(repertory.getAreaCode())
                .subscribe(new Consumer<BaseEntity<STFilterEntity>>() {
                    @Override
                    public void accept(BaseEntity<STFilterEntity> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment3.showPop(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }*/

    /*public void getFilterDistance() {
        repertory.getMaterialDistanceData()
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment3.showDistancePopWindow(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }*/
    public void getFilterDistance(String cid1) {
        DialogHelper.showProgressDialog(fragment3.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getMaterialDistanceData(cid1).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment3.showDistancePopWindow(response.body().getData());
                } else {
                    MToast.showToast(fragment3.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getSTFilterTypeModel(String cid1) {
        DialogHelper.showProgressDialog(fragment3.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getSTFilter(repertory.getAreaCode(), cid1).enqueue(new Callback<BaseEntity<STFilterEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<STFilterEntity>> call, Response<BaseEntity<STFilterEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment3.showPop(response.body().getData());
                } else {
                    MToast.showToast(fragment3.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<STFilterEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
